package net.zdreamer.geracomium.ui.more;

import java.text.ParseException;

import net.zdreamer.geracomium.ui.BaseActivity;
import net.zdreamer.geracomium.utils.PersonalinfoUtils.IdcardUtils;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.geracomium.R;

public class PersonalInformationEditActivity extends BaseActivity implements
        OnClickListener {
    private EditText editinfo;
    private ImageView back, save;
    private TextView titlecontent;
    private Intent mIntent;
    private String getinfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        // TODO Auto-generated method stub
        return R.layout.activity_personalinfoedit;
    }

    @Override
    protected void init() {
        // TODO Auto-generated method stub
        back = (ImageView) findViewById(R.id.back);
        save = (ImageView) findViewById(R.id.share);
        titlecontent = (TextView) findViewById(R.id.title_content);
        editinfo = (EditText) findViewById(R.id.edit_information);

    }

    @Override
    protected void initViews() {
        // TODO Auto-generated method stub
        save.setImageResource(R.drawable.seletor_save);
        Intent intent = getIntent();
        getinfo = intent.getStringExtra("info");
        editinfo.setHint("请输入" + getinfo);
        titlecontent.setText("编辑" + getinfo);
        if (getinfo.equals("联系方式")){
            editinfo.setInputType(InputType.TYPE_CLASS_NUMBER);
        }

    }

    @Override
    protected void initListener() {
        // TODO Auto-generated method stub
        save.setOnClickListener(this);
        back.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.back:
                PersonalInformationEditActivity.this.finish();
                break;
            case R.id.share:
                if (editinfo.getText().toString().equals("")) {
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_LONG).show();
                } else if (getinfo.equals("身份证")) {
                   // editinfo.setInputType(InputType.TYPE_CLASS_NUMBER);
                    try {
                        if (IdcardUtils.IDCardValidate(editinfo.getText()
                                .toString().trim())) {
                            mIntent = new Intent();
                            mIntent.putExtra(getinfo, editinfo.getText().toString());
                            setResult(0, mIntent);
                            this.finish();
                        } else {
                            Toast.makeText(PersonalInformationEditActivity.this,
                                    IdcardUtils.errorInfo, Toast.LENGTH_SHORT)
                                    .show();
                        }
                    } catch (ParseException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    mIntent = new Intent();
                    mIntent.putExtra(getinfo, editinfo.getText().toString());
                    System.out.println(editinfo.getText().toString());
                    setResult(0, mIntent);
                    this.finish();
                }
                break;
            default:
                break;

        }
    }
}
